package xyz.xuminghai.javafx_learn.scene.control;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * 2023/5/20 2:22 星期六<br/>
 * 滑块控件
 *
 * @author xuMingHai
 */
public class SliderApp extends Application {

    private final HBox hBox = new HBox(20.0);

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() {
        Slider slider = new Slider(0.0, 100.0, 0.0);
        slider.setPrefWidth(300.0);
        // 显示刻度线
        slider.setShowTickMarks(true);
        // 显示刻度线标签
        slider.setShowTickLabels(true);
        // 设置刻度线单位
        slider.setMajorTickUnit(10.0);

        Label label = new Label(String.valueOf(slider.valueProperty().intValue()));
        label.setPrefWidth(25.0);
        label.setPadding(new Insets(0.0, 0.0, 20.0, 0.0));
        slider.valueProperty().addListener((observable, oldValue, newValue) -> label.setText(String.valueOf(newValue.intValue())));

        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().addAll(slider, label);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(hBox, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


}
